home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / Miracle C Compiler / miricleC compiler.msi / Instal01.cab / _BE15FD631C6E4A1F85A5BEE17BF34B37 < prev    next >
Encoding:
Text File  |  2000-07-30  |  2.6 KB  |  85 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <assert.h>
  5.  
  6. void xstdlib();
  7.  
  8. int roughly(double x, double y)
  9. {
  10.     if(y==0.0)
  11.        return (fabs(x) < 1.0e-6);
  12.     else
  13.        return (fabs((y-x)/y) < 1.0e-6);
  14. }
  15.  
  16. int main()
  17. {
  18.     xstdlib();
  19. }
  20.  
  21. void xstdlib()
  22. {
  23.     int i,j;
  24.     char *p, buf[50];
  25.  
  26.     printf("==> starting xstdlib <==\n");
  27.  
  28.     // --- first we test the functions defined in strtol.c
  29.     assert(strtol("0xface",&p,0)==0xfaceL && *p=='\0');
  30.     assert(strtol("+f0adg",&p,16)==0xf0adL && *p=='g');
  31.     assert(strtol("-caca0d",&p,13)==-366860L && *p=='d');
  32.  
  33.     assert(strtoul("0xface",&p,0)==0xfaceL && *p=='\0');
  34.     assert(strtoul("+f0adg",&p,16)==0xf0adL && *p=='g');
  35.     assert(strtoul("-caca0d",&p,13)==0L && *p=='-');
  36.  
  37.     assert(atoi("314A")==314 && atoi("-431")==-431);
  38.     assert(atol("0234567")==234567L && atol("-98765")==-98765L);
  39.  
  40.     assert(abs(10)==10 && abs(-10)==10);
  41.     assert(labs(102030L)==102030L && labs(-102030L)==102030L);
  42.     printf("passed strtol/strtoul/atoi/atol/abs/labs...   ");
  43.  
  44.     // --- test itoa and ltoa
  45.     assert(itoa(-3456,buf,10)==buf && strcmp(buf,"-3456")==0);
  46.     assert(itoa(12345,buf,10)==buf && strcmp(buf,"12345")==0);
  47.  
  48.     assert(itoa(065172,buf,8)==buf && strcmp(buf,"65172")==0);
  49.     assert(itoa(0xface,buf,16)==buf && strcmp(buf,"face")==0);
  50.     assert(itoa(-12345,buf,16)==buf && strcmp(buf,"cfc7")==0);
  51.  
  52.     assert(ltoa(123456L,buf,10)==buf && strcmp(buf,"123456")==0);
  53.     assert(ltoa(-213456L,buf,10)==buf && strcmp(buf,"-213456")==0);
  54.  
  55.     assert(ltoa(06532174L,buf,8)==buf && strcmp(buf,"6532174")==0);
  56.     assert(ltoa(0xfaeced23L,buf,16)==buf && strcmp(buf,"faeced23")==0);
  57.     assert(ltoa(0xfaeced23L,buf,10)==buf && strcmp(buf,"-85136093")==0);
  58.     printf("passed itoa/ltoa...\n");
  59.  
  60.     // --- test strtod and atof
  61.     assert(roughly(strtod("469.0123zap",&p),469.0123) && *p=='z');
  62.     assert(roughly(strtod("000000001.23",&p),1.23) && *p=='\0');
  63.     assert(roughly(strtod("00000000100.72",&p),100.72) && *p=='\0');
  64.     assert(roughly(strtod("-3.1415e-123-",&p),-3.1415e-123) && *p=='-');
  65.     assert(roughly(strtod("000001023.0052000000e+0000120A",&p),1.0230052e+123) && *p=='A');
  66.     assert(roughly(strtod("ZX81",&p),0.0) && *p=='Z');
  67.  
  68.     assert(roughly(atof("2.71828"),2.71828));
  69.     assert(roughly(atof("-4.56e-12"),-4.56e-12));
  70.     assert(roughly(atof("0000.005600e10"),5.6e7));
  71.     printf("passed strtod/atof...   ");
  72.  
  73.     for(i=0; i<1000; i++)
  74.         assert((j=rand())>=0 && j<=RAND_MAX);
  75.     srand(3141);
  76.     for(i=0; i<1000; i++)
  77.         assert((j=rand())>=0 && j<=RAND_MAX);
  78.     printf("passed rand/srand...\n");
  79.  
  80.     printf("passed stdlib tests...\n");
  81.     printf("==> finished xstdlib <==\n");
  82.  
  83.     return;
  84. }
  85.